#if MSC_VER >=1000
    #pragma execution_character_set("UTF-8")
#endif

#include "eastsearchsong.h"

#include<QNetworkRequest>
#include<QIODevice>
#include<QNetworkReply>

const auto T_SNG =1;
const auto T_ALB =10;
const auto T_ARTIST =100;
const auto T_SNG_LIST =1000;
const auto T_USER =10002;
const auto EAST_S_URL = "http://music.163.com/api/search/get?s=%s?&limit=%d&type=%d&offset=%d";
EastSearchSong::EastSearchSong(QObject *parent):QObject(parent)
{

}

void EastSearchSong::gSearch(QString url, int type )
{

}

void EastSearchSong::pSearch(QString url,int type)
{
    QNetworkRequest request;
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    QIODevice * p = nullptr;
    QNetworkReply *reply =  manager ->post(request,p);

    // QObject:: connect(reply,SIGNAL(readyRead()),this,SLOT(ex(QNetworkReply*,int)));

    connect(reply,&QIODevice::readyRead,[this,reply,type](){
        auto data =   reply->readAll();
        // 使用from Utf8 模式获取数据
        QString text = QString::fromUtf8(data);
        reply->deleteLater();
        emit  shasData(text,type);
    });
}

void EastSearchSong::gDownload(QString url, QString path)
{
    url ="http://m10.music.126.net/20181011100324/226db695f3caaa7425dc52e132500226/ymusic/1035/0ef5/ec3d/c9030275d6ffae214c18e3c7e7bf03f7.mp3";
    QNetworkRequest request;
    request.setUrl(url);
    request.setHeader(QNetworkRequest::UserAgentHeader,"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/69.0.3497.100 Chrome/69.0.3497.100 Safari/537.36");

    QNetworkReply *reply =manager->get(request);

    connect(manager,&QNetworkAccessManager::finished,[this,url](QNetworkReply *reply){
        auto data =   reply->readAll();

        reply->deleteLater();
        qDebug()<<"download.... url="<<url <<"data "<<data;
    });

    connect(reply,&QNetworkReply::redirected,[](const QUrl &url){
        qDebug()<<"QNetworkReply::redirected"<<url;
    });
    connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(exError (QNetworkReply::NetworkError) ));

    connect(reply,&QNetworkReply::downloadProgress,[this](qint64 bytesReceived, qint64 bytesTotal){
        qDebug()<<bytesReceived <<" - "<< bytesTotal;
    });

    connect(reply,&QIODevice::readyRead,[this,reply,path](){
        auto data =   reply->readAll();

        reply->deleteLater();
        qDebug()<<"download...."<<path <<"data "<<data;

    });
}

void EastSearchSong::ex(QNetworkReply *reply, int type)
{
    auto data =   reply->readAll();
    // utf8 模式
    QString text = QString::fromUtf8( data);
    reply->deleteLater();
    // emit  shasData(text,type);
}

void EastSearchSong::exError(QNetworkReply::NetworkError code)
{
    qDebug()<<"error"<<code;
}


